using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	#endregion
	public class ThuocRepository
	{
		public static List<Thuoc> SelectAll()
		{
			return DataContext.Instance.Thuocs.OrderBy(item => item.TenThuoc).ToList();
		}

		public static Thuoc SelectByID(string mathuoc)
		{
			return DataContext.Instance.Thuocs.FirstOrDefault(item => item.MaThuoc == mathuoc );
		}

		public static bool Insert(Thuoc obj)
		{
			try
			{
				DataContext.Instance.Thuocs.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Delete(string mathuoc)
		{
			try
			{
				var delitem = DataContext.Instance.Thuocs.FirstOrDefault(item => item.MaThuoc == mathuoc );
				DataContext.Instance.Thuocs.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static List<Thuoc> RetrieveByID(string mathuoc)
		{
			return (from item in DataContext.Instance.Thuocs where  item.MaThuoc == mathuoc  select item).ToList();
		}

	}
}